package org.example.struct.array;

import com.alibaba.fastjson.JSON;

/**
 * @author liyishan
 * @date 2023/8/15 10:07
 * @apiNote
 */

public class Test00905SortArrayByParity {
    public static void main(String[] args) {
        int[] nums = {2, 4,5, 3, 5};
        System.out.println(JSON.toJSON(test2(nums)));
    }

    public static int[] test(int[] nums){
        int n = nums.length;
        int index = 0;
        int[] res= new int[n];
        for(int num:nums){
            if(num%2==0){
                res[index++] = num;
            }
        }
        for(int num:nums){
            if(num%2==1){
                res[index++] = num;
            }
        }
        return res;
    }

    public static int[] test1(int[] nums){
        int n = nums.length;
        int[] res = new int[n];
        int left = 0;
        int right = n-1;
        for(int num:nums){
            if(num%2==0){
                res[left++] = num;
            }else{
                res[right--] = num;
            }
        }
        return res;
    }

    public static int[] test2(int[] nums){
        int left = 0;
        int right = nums.length - 1;
        while(left < right){
            while(left<right && nums[left] %2 == 0){
                left++;
            }
            while(left < right && nums[right] %2 ==1){
                right--;
            }
            if (left < right) {
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
                left++;
                right--;
            }
        }
        return nums;
    }

}
